package pers.qianyu.month_202011.date_20201117;

/**
 * 309. 最佳买卖股票时机含冷冻期
 * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/
 *
 * @author mizzle rain
 * @date 2020-11-17 12:59
 */
public class MaxProfitWithFreezingPeriod {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) {
            return 0;
        }
        /**
         * dp[i][0]：当前手里有股票
         * dp[i][1]：当前手里无股票，因为处于冷冻期
         * dp[i][2]：当前手里无股票，并且不处在冷冻期
         */
        int len = prices.length;
        int[][] dp = new int[len][3];
        for (int i = 0; i < len; i++) {
            int p = prices[i];
            if (i == 0) {
                dp[i][0] = -prices[i];
                continue;
            }
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][2] - prices[i]);
            dp[i][1] = dp[i - 1][0] + prices[i];
            dp[i][2] = Math.max(dp[i - 1][1], dp[i - 1][2]);
        }
        return Math.max(dp[len - 1][1], dp[len - 1][2]);
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 0, 2};
        int result = new MaxProfitWithFreezingPeriod().maxProfit(arr);
        System.out.println("result = " + result);
    }
}
